﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.NHibernate.Generic.Support;
using NHibernate.Linq;
using System.Linq.Expressions;

namespace Dao
{
    public class Repository<T> : HibernateDaoSupport, IRepository<T> where T : class
    {
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>实体</returns>
        public virtual T Get(object id)
        {
            return this.HibernateTemplate.Get<T>(Convert.ToInt32(id));
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>实体</returns>
        public virtual T Load(object id)
        {
            return this.HibernateTemplate.Load<T>(id);
        }

        /// <summary>
        /// 插入实体
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>ID</returns>
        public virtual object Save(T entity)
        {
            var result = this.HibernateTemplate.Save(entity);
            return result;
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity">实体</param>
        public virtual void Update(T entity)
        {
            this.HibernateTemplate.Update(entity);
        }

        /// <summary>
        /// 修改或保存实体
        /// </summary>
        /// <param name="entity">实体</param>
        public virtual void SaveOrUpdate(T entity)
        {
            this.HibernateTemplate.SaveOrUpdate(entity);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id">ID</param>
        public virtual void Delete(object id)
        {
            var entity = this.HibernateTemplate.Get<T>(id);
            if (entity == null)
            {
                return;
            }

            this.HibernateTemplate.Delete(entity);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">实体</param>
        public virtual void Delete(T entity)
        {
            if (entity == null)
            {
                return;
            }
            this.HibernateTemplate.Delete(entity);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="IDList">ID集合</param>
        public virtual void Delete(IList<T> IDList)
        {
            foreach (var item in IDList)
            {
                this.HibernateTemplate.Delete(item);
            }
        }

        /// <summary>
        /// 延迟加载类集合
        /// </summary>
        /// <returns></returns>
        public virtual IQueryable<T> LoadAll()
        {
            var result = Session.Query<T>();
            return result;
        }

        /// <summary>
        /// 将对象保存到数据库
        /// </summary>
        public void Flush()
        {
            this.HibernateTemplate.Flush();
        }
    }
}
